package com.cy.utils;

import com.cy.anno.Component;

import java.sql.Connection;
import java.sql.SQLException;

@Component
public class ConnectionUtils {

//    private static ConnectionUtils connectionUtils = new ConnectionUtils();
//
//    /**
//     * 饿汉模式
//     * @return
//     */
//    public static ConnectionUtils getInstance(){
//        return connectionUtils;
//    }

    /**
     * 当前线程的连接
     */
    private ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    public Connection getConnection() throws SQLException {
        Connection connection = threadLocal.get();
        if (connection == null){
            connection = DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }

}
